package com.itheima.mall.api.pms;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mall.dto.BrandDto;
import com.itheima.mall.exception.BusinessException;
import com.itheima.mall.mapper.pms.BrandMapper;
import com.itheima.mall.pojo.pms.Brand;
import com.itheima.mall.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class BrandServiceApiImpl implements BrandServiceApi {
    @Autowired
    private BrandMapper brandMapper;

    @Override
    public void brandCreate(BrandDto brandDto) {
        LambdaQueryWrapper<Brand> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Brand::getName,brandDto.getName());
        Brand brand1 = brandMapper.selectOne(wrapper);
        if (brand1!=null){
            throw new BusinessException("该品牌已存在,不能添加!");
        }
        Brand brand = new Brand();
        BeanUtil.copyProperties(brandDto,brand);
        brand.setProductCount(100);
        brand.setProductCommentCount(100);
        brandMapper.insert(brand);
    }

    @Override
    public void deleteBrand(Long brandId) {
        Brand brand = brandMapper.selectById(brandId);
        if (brand.getShowStatus() == 1){
            throw new BusinessException("当前品牌处于展示状态,不允许删除!");
        }
        brandMapper.deleteById(brandId);
    }

    @Override
    public void deleteBatch(Integer[] ids) {
        if (ids == null || ids.length == 0) {
            throw new BusinessException("未获取到ID");
        }
        for (Integer id : ids) {
            Long brandId = Convert.toLong(id);
            Brand brand = brandMapper.selectById(brandId);
            if (brand.getShowStatus() == 1){
                throw new BusinessException("当前品牌处于展示状态,不允许删除!");
            }
            brandMapper.deleteById(brandId);
        }

    }

    @Override
    public PageResult brandList(Integer pageNum, Integer pageSize, String keyword) {
        PageResult pageResult = new PageResult();
        Page<Brand> pageInfo = new Page<>(pageNum,pageSize);
        //条件构造器
        LambdaQueryWrapper<Brand> wrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        wrapper.like(StrUtil.isNotBlank(keyword),Brand::getName,keyword);
        //添加排序条件
        wrapper.orderByDesc(Brand::getSort);
        Page<Brand> brandPage = brandMapper.selectPage(pageInfo, wrapper);
        pageResult.setPageNum(pageNum);
        pageResult.setPageSize(pageSize);
        pageResult.setList(brandPage.getRecords());
        pageResult.setTotalPage(Convert.toInt(brandPage.getPages()));
        pageResult.setTotal(Convert.toInt(brandPage.getTotal()));
        return pageResult;
    }

    @Override
    public List<Brand> brandListAll() {
        //条件构造器
        LambdaQueryWrapper<Brand> wrapper = new LambdaQueryWrapper<>();
        //添加排序条件
        wrapper.orderByDesc(Brand::getSort);
        List<Brand> list = brandMapper.selectList(wrapper);
        return list;
    }

    @Override
    public void update(Integer id, BrandDto brandDto) {
        Brand brand = new Brand();
        BeanUtil.copyProperties(brandDto,brand);
        brand.setProductCommentCount(100);
        brand.setProductCount(100);
        Long brandId = Convert.toLong(id);
        LambdaQueryWrapper<Brand> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Brand::getId,brandId);
        brandMapper.update(brand,wrapper);
    }

    @Override
    public Brand select(Long brandId) {
        return brandMapper.selectById(brandId);
    }

    @Override
    public void updateFactoryStatus(Integer[] ids, Integer factoryStatus) {
        for (Integer id : ids) {
            Long brandId = Convert.toLong(id);
            Brand brand = brandMapper.selectById(brandId);
            brand.setFactoryStatus(factoryStatus);
            brandMapper.updateById(brand);
        }
    }

    @Override
    public void updateShowStatus(Integer[] ids, Integer showStatus) {
        for (Integer id : ids) {
            Long brandId = Convert.toLong(id);
            Brand brand = brandMapper.selectById(brandId);
            brand.setShowStatus(showStatus);
            brandMapper.updateById(brand);
        }
    }
}
